/*
 * Copyright (C) 2009 M. Hautle.
 * 
 * This file is part of TrackedBean.
 * 
 * TrackedBean is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option) any later version.
 * 
 * TrackedBean is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License along with TrackedBean. If not, see <http://www.gnu.org/licenses/>.
 */
package ch.trackedbean.util;

import java.util.*;

/**
 * {@link IValueAccessor} holding an simple EL expression.
 * 
 * @param <T> The return type
 * @author M. Hautle
 */
public class ELAccessor<T> implements IValueAccessor<T> {
	/** The complete path. */
	private final String path;

	/** The el expression. */
	private final String[] elPath;

	/** Flag register for {@link #elPath} if the bit for the specified index is set then the segment represents an property name. */
	private final long properties;

	/** The type of the returned value. */
	private final Class<T> type;

	/**
	 * Default constructor.
	 * 
	 * @param el An simple EL expression
	 * @param type The type of the value returned by this expression
	 * @throws IllegalArgumentException If the passed expression was illegal
	 */
	public ELAccessor(String el, Class<T> type) throws IllegalArgumentException {
		ELHelper.checkEL(el);
		this.type = type;
		this.path = el;
		// current flag mask
		long bit = 1;
		// we set the first flag cause the first segment must be a property
		long properties = 1;
		final List<String> res = new ArrayList<String>(5);
		for (int p = 0, end = el.length() - 1; p < end;) {
			res.add(el.substring(p, p = ELHelper.getEndIndex(el, p)));
			// last element?
			if (p >= end)
				break;
			// compute next flag
			bit = bit << 1;
			// if the end index is a . then the next atom will be a property
			// skip following '.' (may occour after a index -> a[2].b and in this case the following element is a property)
			if (el.charAt(p) == '.' || el.charAt(++p) == '.') {
				p++;
				// store the property flag for this position
				properties |= bit;
			}
		}
		this.properties = properties;
		this.elPath = res.toArray(new String[res.size()]);
		if (elPath.length > 63)
			throw new IllegalArgumentException("This class supports a maximum of 63 elements inside the expression!");
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String getPath() {
		return path;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	@SuppressWarnings("unchecked")
	public T getValue(Object element, T fallback) {
		// flag mask for the current index
		long mask = 1;
		for (int i = 0; element != null && i < elPath.length; i++) {
			element = (properties & mask) != 0 ? ELHelper.getPropertyValue(elPath[i], element) : ELHelper.getIndexedValue(elPath[i], element);
			mask = mask << 1;
		}
		return element != null ? (T) element : fallback;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public boolean setValue(Object element, T value) {
		final int last = elPath.length - 1;
		// flag mask for the current index
		long mask = 1;
		for (int i = 0; element != null && i < last; i++) {
			element = (properties & mask) != 0 ? ELHelper.getPropertyValue(elPath[i], element) : ELHelper.getIndexedValue(elPath[i], element);
			mask = mask << 1;
		}
		if (element == null)
			return false;
		return ELHelper.setValue(elPath[last], element, value);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public Class<T> getType() {
		return type;
	}
}
